/** @file Loader.qml
@brief The Qt-Complex loader for the dynamic qml document loading mechanism

The \e Loader can be used to load the objects dinamically in the application.
Using the load mechanism the objects are customized with the theme settings
when loaded. The methods of the loader set the theme properties authomatically.
The loader manages all the signals that can be connected with the objects. Depending by the
loaded object the corresponding signal is managed while the other are ignored.
The \e ignoerUnknownSignals property in the \e Connections definitionis set to true
to avoid the errors generated by the unnmanaged signals.

@warning When the loader is instantiated to create an object inside its Item
it should be called \b objLoad Following an internal convention
all the objects expects the loader instantiated with this name.

@authorname \n
@licenseversion \n
@actualversion - @dateupdate

@licensenote
*/

import QtQuick 1.0

Item {
    id: itemLoader

    // Loader properties to be assigned in the program
    property string obj: ""     // Object to load
    property string urlObj:  "" // Object loaded from a url
    property string theme: ""   // Theme name

    property string obj_color:  ""      // Generic color
    property string obj_string: ""      // Generic string assigned to fields, buttons, etc.
    property string obj_message: ""     // Generic message assigned to text
    property string obj_icon: ""        // Generic icon assigned to buttons
    property string obj_background: ""  // Generic background assigned to the object
    property bool obj_flag: false       // Generic flag to enable/disable object options
    property bool obj_info: true        // Flag to enable / disable the info icon for the fields.

    // Set this property if the loader should destroy the object on mouse click event
    property bool destroyAfter: false
    // Geometry dependent properties (default values are overriddend by the applicaiton)
    property int switchHeigth: 45               // Height of every object
    property int fontPoints: 10                 // Base text font size in points
    property int fontSwitches: 8                // Switch text description font size in points
    property int fieldSpacing: 5                // Distance between fields
    property int fieldHeight:  45               // Height of a field (button, switch etc)
    property real elementFactor: 0.40           // The ratio factor of the left object respect the
    // full object size in a compound object

    // The signals connected with the object types
    signal clicked; signal btn1click; signal btn2click; signal btn3click
    signal switched(bool status)

    Loader {
        id: objLoad
        // Inline functions returning the requested property
        function getString() { return itemLoader.obj_string; }
        function getMessage() { return itemLoader.obj_message; }
        function getIcon() { return itemLoader.obj_icon; }
        function getInfoFlag() { return itemLoader.obj_info; }
        function getBackground() { return itemLoader.obj_background; }
        function getTheme() { return itemLoader.theme; }
        function getFlag() { return itemLoader.obj_flag; }
        function getColor() { return itemLoader.obj_color; }

        function objDestroy() { if(destroyAfter == true) { source = ""; } }
        // Gemoetry methods
        function getWidth() { return itemLoader.width; }
        function getHeight() { return itemLoader.height; }
        function getFontPoints() { return itemLoader.fontPoints; }
        function getFontSwitches() { return itemLoader.fontSwitches; }
        function getFieldSpacing() { return itemLoader.fieldSpacing; }
        function getFieldHeight() { return itemLoader.fieldHeight; }
        function getElementFactor() { return itemLoader.elementFactor; }

        // The object source definition
        source: (itemLoader.obj == "") ? itemLoader.urlObj: itemLoader.obj + ".qml";
    }

    // The connections to the possible signals returned by the objects
    Connections {
        target: objLoad.item
        ignoreUnknownSignals: true
        // Signal returned by the objects
        onObjClicked: { itemLoader.clicked(); objLoad.objDestroy() }
        onButton1Clicked: { itemLoader.btn1click(); objLoad.objDestroy() }
        onButton2Clicked: { itemLoader.btn2click(); objLoad.objDestroy() }
        onButton3Clicked: { itemLoader.btn3click(); objLoad.objDestroy() }
        onSwitched: { itemLoader.switched(falgstate); }
    }
}
